﻿/*
' Copyright (c) 2013  Christoc.com
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using Christoc.Modules.CouponsDiscounts.Components;
using System.Web.UI.WebControls;
using System.Data;
using Christoc.Modules.CouponsDiscounts.Data;
using Christoc.Modules.CouponsDiscounts.Components.Controllers;

namespace Christoc.Modules.CouponsDiscounts
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The View class displays the content
    /// 
    /// Typically your view control would be used to display content or functionality in your module.
    /// 
    /// View may be the only control you have in your project depending on the complexity of your module
    /// 
    /// Because the control inherits from CouponsDiscountsModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class View : CouponsDiscountsModuleBase, IActionable
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    LoadGrid();                    
                    var nameControlBuy = "ListProducts.ascx";
                    var cnt = (CouponsDiscountsModuleBase)LoadControl(nameControlBuy);

                    cnt.ModuleConfiguration = ModuleConfiguration;
                    cnt.ID = "listProduct";//System.IO.Path.GetFileNameWithoutExtension(nameControlBuy);
                    listBuySeccion.Controls.Add(cnt);
                }
                var addDiscountControl = "AddDiscount.ascx";
                var moduleLoaded = (CouponsDiscountsModuleBase)LoadControl(addDiscountControl);
                moduleLoaded.ModuleConfiguration = ModuleConfiguration;
                moduleLoaded.ID = System.IO.Path.GetFileNameWithoutExtension(addDiscountControl);
                phAddDiscount.Controls.Add(moduleLoaded);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        private void LoadGrid()
        {
            if (!Page.IsPostBack)
            {
                gvTransactions.DataSource = TransactionController.GetTransactions();
                gvTransactions.DataBind();
            }
        }

        public ModuleActionCollection ModuleActions
        {
            get
            {
                var actions = new ModuleActionCollection
                    {
                        {
                            GetNextActionID(), Localization.GetString("EditModule", LocalResourceFile), "", "", "",
                            EditUrl(), false, SecurityAccessLevel.Edit, true, false
                        }
                    };
                return actions;
            }
        }

       

        protected void lnkBuy_Click(object sender, EventArgs e)
        {
            string idProduct = ((LinkButton)sender).CommandName;

            string script = EditUrl(String.Empty, String.Empty, "Buy", "idProduct=" + idProduct);
            script = script.Replace("550", "250");
            script = script.Replace("950", "250");

            Page.ClientScript.RegisterStartupScript(this.GetType(), "buyProduct", "<script type='text/javascript'> " + script + ";</script>");
        }

    }
}